﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：MyDetailOrder.ascx.cs
//
//
//文件功能描述：顾客的订单详细....
//
//
//创建标识：2007.11.19 李正平
//创建描述：
//
//
//修改标识：2007.11.28 李正平
//修改描述：把URL传替换成页面内传参，增加了对异步操作是否存在异常的判断
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Common_Controls_MyDetailOrder : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["CustomerID"] != null)
            {

                //
                //获取从顾客上页传送过来的数据
                //
                string orderID = string.Empty;

                GetData(ref orderID);

                if (orderID != string.Empty)
                {
                    //
                    //获得订单信息
                    //
                    GetOrderInfo(orderID);
                }
               
            }
        }
    }


    /// <summary>
    /// 获得从上一页传入的数据
    /// </summary>
    /// <returns>订单号</returns>
    protected void GetData(ref string orderID)
    {
        //
        //声四个Label用于接受从一页传入的数据
        //
        Label lblPayStatus = new Label();
        Label lblOrderInfoID = new Label();
        Label lblConsign = new Label();
        Label lblAchive = new Label();



        //
        //判断上一页是否为空
        //
        if (Page.PreviousPage != null)
        {
            try
            {
                //
                //查找母版页
                //
                MasterPage masterPageObj = (MasterPage)Page.PreviousPage.Controls[0];

                //
                //查找容器
                //
                ContentPlaceHolder contentContainer = (ContentPlaceHolder)masterPageObj.FindControl("ContentContext");
                UserControl controlContent = (UserControl)contentContainer.FindControl("MyOrder1");

                //
                //查找到对应的数据传送Label
                //
                lblPayStatus = (Label)controlContent.FindControl("lblPayMoney");
                lblOrderInfoID = (Label)controlContent.FindControl("lblOrderID");
                lblAchive = (Label)controlContent.FindControl("lblAch");
                lblConsign = (Label)controlContent.FindControl("lblCon");
            }
            catch (Exception ex)
            {
                throw ex;

            }

            //
            //获取传入的订单号
            //
            orderID = lblOrderInfoID.Text;

            //
            //判断是否付款
            //
            lblOrderStatus.Text =lblPayStatus.Text;

            //
            //判断是否发货
            //
            lblCon.Text =lblConsign.Text;         

            //
            //判断是否归档
            //
            lblArch.Text = lblAchive.Text; 
        } 

    }


    /// <summary>
    /// 获得订单信息
    /// </summary>
    /// <param name="strOrderID">订单号</param>
    protected void GetOrderInfo(string strOrderID)
    {

        //
        //获取顾客ID
        //
        string customerID = (string)Session["CustomerID"];

        try
        {
            //
            //获取订单详细信息
            //
            localhost.DocumentService orderItem = new localhost.DocumentService();

            //
            //异步调用,返回订单信息
            //
            orderItem.GetOrderItemCompleted += new localhost.GetOrderItemCompletedEventHandler(orderItem_GetOrderItemCompleted);
            orderItem.GetOrderItemAsync(strOrderID);

            //
            //获取订单中收货人信息
            //
            localhost.CustomerService detailOrderInfo = new localhost.CustomerService();

            //
            //异步调用,返回订单详细信息
            //
            detailOrderInfo.GetOrderInfoCompleted += new localhost.GetOrderInfoCompletedEventHandler(detailOrderInfo_GetOrderInfoCompleted);
            detailOrderInfo.GetOrderInfoAsync(strOrderID);

            //
            //获取顾客详细信息
            //
            localhost.CustomerService customerDetailInfo = new localhost.CustomerService();
            customerDetailInfo.GetCustomerDetailInfoCompleted += new localhost.GetCustomerDetailInfoCompletedEventHandler(customerDetailInfo_GetCustomerDetailInfoCompleted);

            //
            //返回顾客信息
            //
            customerDetailInfo.GetCustomerDetailInfoAsync(customerID);
        }
        catch (Exception ex)
        {
            //
            //抛出异常
            //
            throw ex;
        }
    }

    /// <summary>
    /// 异步调用结果获取购买人的详细信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void customerDetailInfo_GetCustomerDetailInfoCompleted(object sender, localhost.GetCustomerDetailInfoCompletedEventArgs e)
    {

        //
        //判断异步操作是否存在异常
        //
        if (e.Error == null)
        {
            //
            //新建一个顾客实例,获取顾客的详细信息
            //          
            localhost.Customer customerInfo = new localhost.Customer();

            //
            //获取结果
            //
            customerInfo = e.Result;

            lblShopperName.Text = customerInfo.FirstName + " " + customerInfo.LastName;

            lblLoginName.Text = customerInfo.Account;

            lblShopperPhone.Text = customerInfo.Phone;

            lblShopperAddr.Text = customerInfo.Address;

            lblShopperEmail.Text = customerInfo.Email;

            lblShopperZip.Text = customerInfo.Zip;

        }
        else
        {
            //
            //异常
            //
            throw e.Error;
        }
    }


    /// <summary>
    /// 获取订单中商品详细信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void orderItem_GetOrderItemCompleted(object sender, localhost.GetOrderItemCompletedEventArgs e)
    {
        //
        //判断异步操作过程是否存在异常
        //
        if (e.Error == null)
        {
            //
            //绑定输出订单列表
            //
            gvResult.DataSource = e.Result;
            gvResult.DataBind();

            //
            //声明一个商品总量
            //
            double totalNum = 0;

            //
            //声明商品总金额
            //
            decimal totalPrice = 0.0M;

            try
            {
                //
                //获得总量和总价
                //
                foreach (GridViewRow dr in gvResult.Rows)
                {
                    //
                    //获取GridView的数量列，累加
                    //
                    totalNum += Convert.ToInt64(dr.Cells[3].Text.Trim());

                    //
                    //获取GridView的商品总价列，累加
                    //
                    totalPrice += Convert.ToDecimal(((Label)dr.Cells[4].FindControl("lblTotalPrice")).Text.Trim());
                }
            }
            catch (Exception ex)
            {
                //
                //异常
                //
                throw ex;
            }

            //
            //商品总量
            //
            lblTotalNum.Text = totalNum.ToString();

            //
            //付款总计
            //
            lblTotalPrice.Text = totalPrice.ToString();

            //
            //商品总金额
            //
            lblProductTotalPrices.Text = totalPrice.ToString();

            //
            //订单金额
            //
            lblOrderTotalPrices.Text = totalPrice.ToString();
        }
        else
        {
            //
            //异常
            //
            throw e.Error;
        }

    }



    /// <summary>
    /// 获取订单中收货人的详细信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void detailOrderInfo_GetOrderInfoCompleted(object sender, localhost.GetOrderInfoCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            //
            //获得返回值
            //
            localhost.OrderInfo customerOrderInfo = new localhost.OrderInfo();

            //
            //获得异步调用结果
            //
            customerOrderInfo = e.Result;

            //
            //获取订单号
            //
            lblOrderNum.Text = customerOrderInfo.OrderInfoID;

            //
            //下订单时间 
            //
            lblOrderTime.Text = customerOrderInfo.CreationTime.ToShortDateString();

            //
            //收货人姓名
            //
            lblReceiverName.Text = customerOrderInfo.ShipToFirstName + " " + customerOrderInfo.ShipToLastName;

            //
            //收货人邮编
            //
            lblReceiveZip.Text = customerOrderInfo.ShipZip;

            //
            //收货人地址
            //
            lblReceiveAddr.Text = customerOrderInfo.ShipAddress;

            //
            //收货人电话
            //
            lblReceivePhone.Text = customerOrderInfo.Phone;

            //
            //获取支付方式
            //
            lblPayWay.Text = customerOrderInfo.BillMethod;

        }
        else
        {
            //
            //异常
            //
            throw e.Error;
        }
    }


    /// <summary>
    /// 返回我的订单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>   
    protected void ibtnGoBack_Click(object sender, ImageClickEventArgs e)
    {
        //
        //返回我的订单
        //
        Server.Transfer("MyOrder.aspx");
    }

}

